package com.hmall.search.web;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.search.pojo.ItemDoc;
import com.hmall.search.pojo.RequestParams;
import com.hmall.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description SearchController
 * @Author GGBond
 * @Date 2024-11-15
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ItemClient itemClient;


    /**
     * 过滤项聚合
     *
     * @param requestParams
     * @return
     */
    @PostMapping("/filters")
    public JSONObject filters(@RequestBody RequestParams requestParams) {
        List<Item> itemList = searchService.list();
        Set<String> categoryList = new HashSet<>();
        Map<String, Set<String>> map = new HashMap<>();
        for (Item item : itemList) {
            categoryList.add(item.getCategory());
        }
        Set<String> brandList = new HashSet<>();
        for (Item item : itemList) {
            brandList.add(item.getBrand());
        }
        map.put("category", categoryList);
        map.put("brand", brandList);
        return JSONUtil.parseObj(map);
    }


    /**
     * 基本搜索功能
     * @param requestParams
     * @return
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> list(@RequestBody RequestParams requestParams) {

        return searchService.search(requestParams);
    }
}
